﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MemCached;

namespace BusinessModel.StudyManager
{
    public class CourseWareCateHelper
    {
        private string mcName = "el_coursewarecategory";
        private CMemCached mc = new CMemCached();

        public List<CourseWareCateEn> GetAllCourseWareCateEn()
        {
            List<CourseWareCateEn> resCourseWareCateEn;
            //缓存判断
            if (!mc.KeyExist(mcName))
            {
                //从数据库获取数据
                EntityOprHelper enOp = new EntityOprHelper();

                resCourseWareCateEn = enOp.GetEntityList<CourseWareCateEn>(string.Empty);
                mc.SetObject(mcName, resCourseWareCateEn);
            }
            else
            {
                resCourseWareCateEn = mc.GetObject(mcName) as List<CourseWareCateEn>;
            }

            return resCourseWareCateEn;
        }

        public string AddCourseWareCate(CourseWareCateEn ccEn)
        {
            string message = "添加成功！";
            EntityOprHelper eoHelper = new EntityOprHelper();
            CourseWareCateEn en = eoHelper.GetEntity<CourseWareCateEn>(ccEn.category_id);
            if (en != null)
                message = "课程分类编码已存在！";
            bool flag = eoHelper.AddEntity<CourseWareCateEn>(ccEn);
            if (!flag)
                message = "添加失败！";
            return message;
        }

        public string EditCourseWareCate(CourseWareCateEn ccEn)
        {
            string message = "修改成功！";
            EntityOprHelper eoHelper = new EntityOprHelper();
            CourseWareCateEn en = eoHelper.GetEntity<CourseWareCateEn>(ccEn.category_id);
            if (en == null)
                message = "该记录已不存在！";
            bool flag = eoHelper.EditEntity<CourseWareCateEn>(ccEn);
            if (!flag)
                message = "修改失败！";
            return message;
        }

        public string DeleteCourseWareCate(string cateID)
        {
            string message = "删除成功！";
            EntityOprHelper eoHelper = new EntityOprHelper();
            string sql = string.Format("Delete from el_coursewarecategory Where category_id = '{0}' or parent_id='{0}'",
                cateID);
            if (!eoHelper.ExecuteNonQuery(sql))
            {
                message = "删除失败！";
            }
            return message;
        }
    }
}
